/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-06-08     Administrator       the first version
 */
#include <rtthread.h>
#include <rtdevice.h>
#include <board.h>
#include <RGB_LED.h>


/* 有效电平配置 (根据实际硬件修改) */
#define LED_ON_LEVEL  0  // 0=低电平点亮, 1=高电平点亮
#define LED_OFF_LEVEL (!LED_ON_LEVEL)

/* LED初始化函数 */
void led_init(void)
{
    /* 配置引脚为输出模式 */
    rt_pin_mode(LED_R_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_G_PIN, PIN_MODE_OUTPUT);
    rt_pin_mode(LED_B_PIN, PIN_MODE_OUTPUT);

    /* 默认关闭所有LED */
    rt_pin_write(LED_R_PIN, LED_OFF_LEVEL);
    rt_pin_write(LED_G_PIN, LED_OFF_LEVEL);
    rt_pin_write(LED_B_PIN, LED_OFF_LEVEL);
}

/* 控制单个LED */
void led_control(rt_base_t pin, rt_base_t state)
{
    rt_pin_write(pin, state ? LED_ON_LEVEL : LED_OFF_LEVEL);
}

/* 控制RGB三色灯 */
void rgb_led_control(int r, int g, int b)
{
    rt_pin_write(LED_R_PIN, r ? LED_ON_LEVEL : LED_OFF_LEVEL);
    rt_pin_write(LED_G_PIN, g ? LED_ON_LEVEL : LED_OFF_LEVEL);
    rt_pin_write(LED_B_PIN, b ? LED_ON_LEVEL : LED_OFF_LEVEL);
}

/* 测试函数 */
void led_test(void)
{
    led_init();  // 初始化LED

    while (1) {
        // 红灯亮
        led_control(LED_R_PIN, 1);
        rt_thread_mdelay(500);

        // 绿灯亮
        led_control(LED_G_PIN, 1);
        rt_thread_mdelay(500);

        // 蓝灯亮
        led_control(LED_B_PIN, 1);
        rt_thread_mdelay(500);

        // 黄灯(红+绿)
        rgb_led_control(1, 1, 0);
        rt_thread_mdelay(500);

        // 紫灯(红+蓝)
        rgb_led_control(1, 0, 1);
        rt_thread_mdelay(500);

        // 青灯(绿+蓝)
        rgb_led_control(0, 1, 1);
        rt_thread_mdelay(500);

        // 白灯(全亮)
        rgb_led_control(1, 1, 1);
        rt_thread_mdelay(500);

        // 全部关闭
        rgb_led_control(0, 0, 0);
        rt_thread_mdelay(500);
    }
}

/* 导出到MSH命令 */
MSH_CMD_EXPORT(led_test, LED test command);
